home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / RCS_56.ARJ / RCS.C < prev    next >
C/C++ Source or Header  |  1992-02-11  |  44KB  |  1,555 lines

  1. /*
  2.  *                      RCS create/change operation
  3.  */
  4. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  5.    Copyright 1990, 1991 by Paul Eggert
  6.    Distributed under license by the Free Software Foundation, Inc.
  7.  
  8. This file is part of RCS.
  9.  
  10. RCS is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. RCS is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with RCS; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. Report problems and direct all questions to:
  25.  
  26.     rcs-bugs@cs.purdue.edu
  27.  
  28. */
  29.  
  30.  
  31.  
  32.  
  33. /* $Log: rcs.c,v $
  34.  * Revision 5.12  1991/11/20  17:58:08  eggert
  35.  * Don't read the delta tree from a nonexistent RCS file.
  36.  *
  37.  * Revision 5.11  1991/10/07  17:32:46  eggert
  38.  * Remove lint.
  39.  *
  40.  * Revision 5.10  1991/08/19  23:17:54  eggert
  41.  * Add -m, -r$, piece tables.  Revision separator is `:', not `-'.  Tune.
  42.  *
  43.  * Revision 5.9  1991/04/21  11:58:18  eggert
  44.  * Add -x, RCSINIT, MS-DOS support.
  45.  *
  46.  * Revision 5.8  1991/02/25  07:12:38  eggert
  47.  * strsave -> str_save (DG/UX name clash)
  48.  * 0444 -> S_IRUSR|S_IRGRP|S_IROTH for portability
  49.  *
  50.  * Revision 5.7  1990/12/18  17:19:21  eggert
  51.  * Fix bug with multiple -n and -N options.
  52.  *
  53.  * Revision 5.6  1990/12/04  05:18:40  eggert
  54.  * Use -I for prompts and -q for diagnostics.
  55.  *
  56.  * Revision 5.5  1990/11/11  00:06:35  eggert
  57.  * Fix `rcs -e' core dump.
  58.  *
  59.  * Revision 5.4  1990/11/01  05:03:33  eggert
  60.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  61.  *
  62.  * Revision 5.3  1990/10/04  06:30:16  eggert
  63.  * Accumulate exit status across files.
  64.  *
  65.  * Revision 5.2  1990/09/04  08:02:17  eggert
  66.  * Standardize yes-or-no procedure.
  67.  *
  68.  * Revision 5.1  1990/08/29  07:13:51  eggert
  69.  * Remove unused setuid support.  Clean old log messages too.
  70.  *
  71.  * Revision 5.0  1990/08/22  08:12:42  eggert
  72.  * Don't lose names when applying -a option to multiple files.
  73.  * Remove compile-time limits; use malloc instead.  Add setuid support.
  74.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  75.  * Ansify and Posixate.  Add -V.  Fix umask bug.  Make linting easier.  Tune.
  76.  * Yield proper exit status.  Check diff's output.
  77.  *
  78.  * Revision 4.11  89/05/01  15:12:06  narten
  79.  * changed copyright header to reflect current distribution rules
  80.  * 
  81.  * Revision 4.10  88/11/08  16:01:54  narten
  82.  * didn't install previous patch correctly
  83.  * 
  84.  * Revision 4.9  88/11/08  13:56:01  narten
  85.  * removed include <sysexits.h> (not needed)
  86.  * minor fix for -A option
  87.  * 
  88.  * Revision 4.8  88/08/09  19:12:27  eggert
  89.  * Don't access freed storage.
  90.  * Use execv(), not system(); yield proper exit status; remove lint.
  91.  * 
  92.  * Revision 4.7  87/12/18  11:37:17  narten
  93.  * lint cleanups (Guy Harris)
  94.  * 
  95.  * Revision 4.6  87/10/18  10:28:48  narten
  96.  * Updating verison numbers. Changes relative to 1.1 are actually 
  97.  * relative to 4.3
  98.  * 
  99.  * Revision 1.4  87/09/24  13:58:52  narten
  100.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  101.  * warnings)
  102.  * 
  103.  * Revision 1.3  87/03/27  14:21:55  jenkins
  104.  * Port to suns
  105.  * 
  106.  * Revision 1.2  85/12/17  13:59:09  albitz
  107.  * Changed setstate to rcs_setstate because of conflict with random.o.
  108.  * 
  109.  * Revision 4.3  83/12/15  12:27:33  wft
  110.  * rcs -u now breaks most recent lock if it can't find a lock by the caller.
  111.  * 
  112.  * Revision 4.2  83/12/05  10:18:20  wft
  113.  * Added conditional compilation for sending mail.
  114.  * Alternatives: V4_2BSD, V6, USG, and other.
  115.  * 
  116.  * Revision 4.1  83/05/10  16:43:02  wft
  117.  * Simplified breaklock(); added calls to findlock() and getcaller().
  118.  * Added option -b (default branch). Updated -s and -w for -b.
  119.  * Removed calls to stat(); now done by pairfilenames().
  120.  * Replaced most catchints() calls with restoreints().
  121.  * Removed check for exit status of delivermail().
  122.  * Directed all interactive output to stderr.
  123.  * 
  124.  * Revision 3.9.1.1  83/12/02  22:08:51  wft
  125.  * Added conditional compilation for 4.2 sendmail and 4.1 delivermail.
  126.  * 
  127.  * Revision 3.9  83/02/15  15:38:39  wft
  128.  * Added call to fastcopy() to copy remainder of RCS file.
  129.  *
  130.  * Revision 3.8  83/01/18  17:37:51  wft
  131.  * Changed sendmail(): now uses delivermail, and asks whether to break the lock.
  132.  *
  133.  * Revision 3.7  83/01/15  18:04:25  wft
  134.  * Removed putree(); replaced with puttree() in rcssyn.c.
  135.  * Combined putdellog() and scanlogtext(); deleted putdellog().
  136.  * Cleaned up diagnostics and error messages. Fixed problem with
  137.  * mutilated files in case of deletions in 2 files in a single command.
  138.  * Changed marking of selector from 'D' to DELETE.
  139.  *
  140.  * Revision 3.6  83/01/14  15:37:31  wft
  141.  * Added ignoring of interrupts while new RCS file is renamed;
  142.  * Avoids deletion of RCS files by interrupts.
  143.  *
  144.  * Revision 3.5  82/12/10  21:11:39  wft
  145.  * Removed unused variables, fixed checking of return code from diff,
  146.  * introduced variant COMPAT2 for skipping Suffix on -A files.
  147.  *
  148.  * Revision 3.4  82/12/04  13:18:20  wft
  149.  * Replaced getdelta() with gettree(), changed breaklock to update
  150.  * field lockedby, added some diagnostics.
  151.  *
  152.  * Revision 3.3  82/12/03  17:08:04  wft
  153.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  154.  * /usr/ucb/Mail with macro MAIL. Removed handling of Suffix (-x).
  155.  * fixed -u for missing revno. Disambiguated structure members.
  156.  *
  157.  * Revision 3.2  82/10/18  21:05:07  wft
  158.  * rcs -i now generates a file mode given by the umask minus write permission;
  159.  * otherwise, rcs keeps the mode, but removes write permission.
  160.  * I added a check for write error, fixed call to getlogin(), replaced
  161.  * curdir() with getfullRCSname(), cleaned up handling -U/L, and changed
  162.  * conflicting, long identifiers.
  163.  *
  164.  * Revision 3.1  82/10/13  16:11:07  wft
  165.  * fixed type of variables receiving from getc() (char -> int).
  166.  */
  167.  
  168.  
  169. #include "rcsbase.h"
  170.  
  171. struct  Lockrev {
  172.     char const *revno;
  173.         struct  Lockrev   * nextrev;
  174. };
  175.  
  176. struct  Symrev {
  177.     char const *revno;
  178.     char const *ssymbol;
  179.         int     override;
  180.         struct  Symrev  * nextsym;
  181. };
  182.  
  183. struct Message {
  184.     char const *revno;
  185.     struct cbuf message;
  186.     struct Message *nextmessage;
  187. };
  188.  
  189. struct  Status {
  190.     char const *revno;
  191.     char const *status;
  192.         struct  Status  * nextstatus;
  193. };
  194.  
  195. enum changeaccess {append, erase};
  196. struct chaccess {
  197.     char const *login;
  198.     enum changeaccess command;
  199.     struct chaccess *nextchaccess;
  200. };
  201.  
  202. struct delrevpair {
  203.     char const *strt;
  204.     char const *end;
  205.         int     code;
  206. };
  207.  
  208. static int buildeltatext P((struct hshentries const*));
  209. static int removerevs P((void));
  210. static int sendmail P((char const*,char const*));
  211. static struct Lockrev *rmnewlocklst P((struct Lockrev const*));
  212. static void breaklock P((struct hshentry const*));
  213. static void buildtree P((void));
  214. static void cleanup P((void));
  215. static void doaccess P((void));
  216. static void doassoc P((void));
  217. static void dolocks P((void));
  218. static void domessages P((void));
  219. static void getaccessor P((char*,enum changeaccess));
  220. static void getassoclst P((int,char*));
  221. static void getchaccess P((char const*,enum changeaccess));
  222. static void getdelrev P((char*));
  223. static void getmessage P((char*));
  224. static void getstates P((char*));
  225. static void rcs_setstate P((char const*,char const*));
  226. static void scanlogtext P((struct hshentry*,int));
  227. static void setlock P((char const*));
  228.  
  229. static struct buf numrev;
  230. static char const *headstate;
  231. static int chgheadstate, exitstatus, lockhead, unlockcaller;
  232. static struct Lockrev *newlocklst, *rmvlocklst;
  233. static struct Message *messagelst, *lastmessage;
  234. static struct Status *statelst, *laststate;
  235. static struct Symrev *assoclst, *lastassoc;
  236. static struct chaccess *chaccess, **nextchaccess;
  237. static struct delrevpair delrev;
  238. static struct hshentry *cuthead, *cuttail, *delstrt;
  239. static struct hshentries *gendeltas;
  240.  
  241. mainProg(rcsId, "rcs", "$Id: rcs.c,v 5.12 1991/11/20 17:58:08 eggert Exp $")
  242. {
  243.     static char const cmdusage[] =
  244.         "\nrcs usage: rcs -{ae}logins -Afile -{blu}[rev] -cstring -{iLU} -{nNs}name[:rev] -orange -t[file] -Vn file ...";
  245.  
  246.     char *a, **newargv, *textfile;
  247.     char const *branchsym, *commsyml;
  248.     int branchflag, expmode, initflag;
  249.     int e, r, strictlock, strict_selected, textflag;
  250.     mode_t defaultRCSmode;    /* default mode for new RCS files */
  251.     mode_t RCSmode;
  252.     struct buf branchnum;
  253.     struct stat workstat;
  254.         struct  Lockrev *curlock,  * rmvlock, *lockpt;
  255.         struct  Status  * curstate;
  256.  
  257.     nosetid();
  258.  
  259.     nextchaccess = &chaccess;
  260.     branchsym = commsyml = textfile = nil;
  261.     branchflag = strictlock = false;
  262.     bufautobegin(&branchnum);
  263.     curlock = rmvlock = nil;
  264.     defaultRCSmode = 0;
  265.     expmode = -1;
  266.     suffixes = X_DEFAULT;
  267.         initflag= textflag = false;
  268.         strict_selected = 0;
  269.  
  270.         /*  preprocessing command options    */
  271.     argc = getRCSINIT(argc, argv, &newargv);
  272.     argv = newargv;
  273.     while (a = *++argv,  0<--argc && *a++=='-') {
  274.         switch (*a++) {
  275.  
  276.         case 'i':   /*  initial version  */
  277.                         initflag = true;
  278.                         break;
  279.  
  280.                 case 'b':  /* change default branch */
  281.             if (branchflag) redefined('b');
  282.                         branchflag= true;
  283.             branchsym = a;
  284.                         break;
  285.  
  286.                 case 'c':   /*  change comment symbol   */
  287.             if (commsyml) redefined('c');
  288.             commsyml = a;
  289.                         break;
  290.  
  291.                 case 'a':  /*  add new accessor   */
  292.             getaccessor(*argv+1, append);
  293.                         break;
  294.  
  295.                 case 'A':  /*  append access list according to accessfile  */
  296.             if (!*a) {
  297.                 error("missing file name after -A");
  298.                             break;
  299.                         }
  300.             *argv = a;
  301.             if (0 < pairfilenames(1,argv,rcsreadopen,true,false)) {
  302.                 while (AccessList) {
  303.                 getchaccess(str_save(AccessList->login),append);
  304.                 AccessList = AccessList->nextaccess;
  305.                 }
  306.                 Izclose(&finptr);
  307.                         }
  308.                         break;
  309.  
  310.                 case 'e':    /*  remove accessors   */
  311.             getaccessor(*argv+1, erase);
  312.                         break;
  313.  
  314.                 case 'l':    /*   lock a revision if it is unlocked   */
  315.             if (!*a) {
  316.                 /* Lock head or default branch.  */
  317.                             lockhead = true;
  318.                             break;
  319.                         }
  320.             lockpt = talloc(struct Lockrev);
  321.             lockpt->revno = a;
  322.                         lockpt->nextrev = nil;
  323.                         if ( curlock )
  324.                             curlock->nextrev = lockpt;
  325.                         else
  326.                             newlocklst = lockpt;
  327.                         curlock = lockpt;
  328.                         break;
  329.  
  330.                 case 'u':   /*  release lock of a locked revision   */
  331.             if (!*a) {
  332.                             unlockcaller=true;
  333.                             break;
  334.                         }
  335.             lockpt = talloc(struct Lockrev);
  336.             lockpt->revno = a;
  337.                         lockpt->nextrev = nil;
  338.                         if (rmvlock)
  339.                             rmvlock->nextrev = lockpt;
  340.                         else
  341.                             rmvlocklst = lockpt;
  342.                         rmvlock = lockpt;
  343.  
  344.                         curlock = rmnewlocklst(lockpt);
  345.                         break;
  346.  
  347.                 case 'L':   /*  set strict locking */
  348.                         if (strict_selected++) {  /* Already selected L or U? */
  349.                if (!strictlock)      /* Already selected -U? */
  350.                    warn("-L overrides -U.");
  351.                         }
  352.                         strictlock = true;
  353.                         break;
  354.  
  355.                 case 'U':   /*  release strict locking */
  356.                         if (strict_selected++) {  /* Already selected L or U? */
  357.                if (strictlock)      /* Already selected -L? */
  358.                    warn("-L overrides -U.");
  359.                         }
  360.             else
  361.                 strictlock = false;
  362.                         break;
  363.  
  364.                 case 'n':    /*  add new association: error, if name exists */
  365.             if (!*a) {
  366.                 error("missing symbolic name after -n");
  367.                             break;
  368.                         }
  369.                         getassoclst(false, (*argv)+1);
  370.                         break;
  371.  
  372.                 case 'N':   /*  add or change association   */
  373.             if (!*a) {
  374.                 error("missing symbolic name after -N");
  375.                             break;
  376.                         }
  377.                         getassoclst(true, (*argv)+1);
  378.                         break;
  379.  
  380.         case 'm':   /*  change log message  */
  381.             getmessage(a);
  382.             break;
  383.  
  384.         case 'o':   /*  delete revisions  */
  385.             if (delrev.strt) redefined('o');
  386.             if (!*a) {
  387.                 error("missing revision range after -o");
  388.                             break;
  389.                         }
  390.                         getdelrev( (*argv)+1 );
  391.                         break;
  392.  
  393.                 case 's':   /*  change state attribute of a revision  */
  394.             if (!*a) {
  395.                 error("state missing after -s");
  396.                             break;
  397.                         }
  398.                         getstates( (*argv)+1);
  399.                         break;
  400.  
  401.                 case 't':   /*  change descriptive text   */
  402.                         textflag=true;
  403.             if (*a) {
  404.                 if (textfile) redefined('t');
  405.                 textfile = a;
  406.                         }
  407.                         break;
  408.  
  409.         case 'I':
  410.             interactiveflag = true;
  411.             break;
  412.  
  413.                 case 'q':
  414.                         quietflag = true;
  415.                         break;
  416.  
  417.         case 'x':
  418.             suffixes = a;
  419.             break;
  420.  
  421.         case 'V':
  422.             setRCSversion(*argv);
  423.             break;
  424.  
  425.         case 'k':    /*  set keyword expand mode  */
  426.             if (0 <= expmode) redefined('k');
  427.             if (0 <= (expmode = str2expmode(a)))
  428.                 break;
  429.             /* fall into */
  430.                 default:
  431.             faterror("unknown option: %s%s", *argv, cmdusage);
  432.                 };
  433.         }  /* end processing of options */
  434.  
  435.     if (argc<1) faterror("no input file%s", cmdusage);
  436.         if (nerror) {
  437.         diagnose("%s aborted\n",cmdid);
  438.         exitmain(EXIT_FAILURE);
  439.         }
  440.     if (initflag) {
  441.         defaultRCSmode = umask((mode_t)0);
  442.         VOID umask(defaultRCSmode);
  443.         defaultRCSmode = (S_IRUSR|S_IRGRP|S_IROTH) & ~defaultRCSmode;
  444.     }
  445.  
  446.         /* now handle all filenames */
  447.         do {
  448.     ffree();
  449.  
  450.         if ( initflag ) {
  451.         switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  452.                 case -1: break;        /*  not exist; ok */
  453.                 case  0: continue;     /*  error         */
  454.                 case  1: error("file %s exists already", RCSfilename);
  455.                          continue;
  456.             }
  457.     }
  458.         else  {
  459.         switch (pairfilenames(argc, argv, rcswriteopen, true, false)) {
  460.                 case -1: continue;    /*  not exist      */
  461.                 case  0: continue;    /*  errors         */
  462.                 case  1: break;       /*  file exists; ok*/
  463.             }
  464.     }
  465.  
  466.  
  467.         /* now RCSfilename contains the name of the RCS file, and
  468.          * workfilename contains the name of the working file.
  469.          * if !initflag, finptr contains the file descriptor for the
  470.          * RCS file. The admin node is initialized.
  471.          */
  472.  
  473.     diagnose("RCS file: %s\n", RCSfilename);
  474.  
  475.     RCSmode = defaultRCSmode;
  476.     if (initflag) {
  477.         if (stat(workfilename, &workstat) == 0)
  478.             RCSmode = workstat.st_mode;
  479.     } else {
  480.         if (!checkaccesslist()) continue;
  481.         gettree(); /* Read the delta tree.  */
  482.         RCSmode = RCSstat.st_mode;
  483.     }
  484.     RCSmode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
  485.  
  486.         /*  update admin. node    */
  487.         if (strict_selected) StrictLocks = strictlock;
  488.     if (commsyml) {
  489.         Comment.string = commsyml;
  490.         Comment.size = strlen(commsyml);
  491.     }
  492.     if (0 <= expmode) Expand = expmode;
  493.  
  494.         /* update default branch */
  495.     if (branchflag && expandsym(branchsym, &branchnum)) {
  496.         if (countnumflds(branchnum.string)) {
  497.         Dbranch = branchnum.string;
  498.             } else
  499.                 Dbranch = nil;
  500.         }
  501.  
  502.     doaccess();    /* Update access list.  */
  503.  
  504.     doassoc();    /* Update association list.  */
  505.  
  506.     dolocks();    /* Update locks.  */
  507.  
  508.     domessages();    /* Update log messages.  */
  509.  
  510.         /*  update state attribution  */
  511.         if (chgheadstate) {
  512.             /* change state of default branch or head */
  513.             if (Dbranch==nil) {
  514.                 if (Head==nil)
  515.              warn("can't change states in an empty tree");
  516.                 else Head->state = headstate;
  517.             } else {
  518.         rcs_setstate(Dbranch,headstate); /* Can't set directly */
  519.             }
  520.         }
  521.         curstate = statelst;
  522.         while( curstate ) {
  523.             rcs_setstate(curstate->revno,curstate->status);
  524.             curstate = curstate->nextstatus;
  525.         }
  526.  
  527.         cuthead = cuttail = nil;
  528.     if (delrev.strt && removerevs()) {
  529.             /*  rebuild delta tree if some deltas are deleted   */
  530.             if ( cuttail )
  531.         VOID genrevs(cuttail->num, (char *)nil,(char *)nil,
  532.                  (char *)nil, &gendeltas);
  533.             buildtree();
  534.         }
  535.  
  536.     if (nerror)
  537.         continue;
  538.  
  539.         putadmin(frewrite);
  540.         if ( Head )
  541.            puttree(Head, frewrite);
  542.     putdesc(textflag,textfile);
  543.  
  544.         if ( Head) {
  545.         if (!delrev.strt && !messagelst) {
  546.         /* No revision was deleted and no message was changed.  */
  547.         fastcopy(finptr, frewrite);
  548.             } else {
  549.         if (!cuttail || buildeltatext(gendeltas)) {
  550.             advise_access(finptr, MADV_SEQUENTIAL);
  551.             scanlogtext((struct hshentry *)nil, false);
  552.                     /* copy rest of delta text nodes that are not deleted      */
  553.         }
  554.             }
  555.         }
  556.     Izclose(&finptr);
  557.         if ( ! nerror ) {  /*  move temporary file to RCS file if no error */
  558.         /* update mode */
  559.         ignoreints();
  560.         r = chnamemod(&frewrite, newRCSfilename, RCSfilename, RCSmode);
  561.         e = errno;
  562.         keepdirtemp(newRCSfilename);
  563.         restoreints();
  564.         if (r != 0) {
  565.         enerror(e, RCSfilename);
  566.         error("saved in %s", newRCSfilename);
  567.         dirtempunlink();
  568.                 break;
  569.             }
  570.         diagnose("done\n");
  571.         } else {
  572.         diagnose("%s aborted; %s unchanged.\n",cmdid,RCSfilename);
  573.         }
  574.     } while (cleanup(),
  575.                  ++argv, --argc >=1);
  576.  
  577.     tempunlink();
  578.     exitmain(exitstatus);
  579. }       /* end of main (rcs) */
  580.  
  581.     static void
  582. cleanup()
  583. {
  584.     if (nerror) exitstatus = EXIT_FAILURE;
  585.     Izclose(&finptr);
  586.     Ozclose(&fcopy);
  587.     Ozclose(&frewrite);
  588.     dirtempunlink();
  589. }
  590.  
  591.     exiting void
  592. exiterr()
  593. {
  594.     dirtempunlink();
  595.     tempunlink();
  596.     _exit(EXIT_FAILURE);
  597. }
  598.  
  599.  
  600.     static void
  601. getassoclst(flag, sp)
  602. int     flag;
  603. char    * sp;
  604. /*  Function:   associate a symbolic name to a revision or branch,      */
  605. /*              and store in assoclst                                   */
  606.  
  607. {
  608.         struct   Symrev  * pt;
  609.     char const *temp;
  610.         int                c;
  611.  
  612.         while( (c=(*++sp)) == ' ' || c == '\t' || c =='\n')  ;
  613.         temp = sp;
  614.     sp = checkid(sp, ':');  /*  check for invalid symbolic name  */
  615.     c = *sp;   *sp = '\0';
  616.         while( c == ' ' || c == '\t' || c == '\n')  c = *++sp;
  617.  
  618.         if ( c != ':' && c != '\0') {
  619.         error("invalid string %s after option -n or -N",sp);
  620.             return;
  621.         }
  622.  
  623.     pt = talloc(struct Symrev);
  624.         pt->ssymbol = temp;
  625.         pt->override = flag;
  626.     if (c == '\0')  /*  delete symbol  */
  627.             pt->revno = nil;
  628.         else {
  629.             while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  630.         pt->revno = sp;
  631.         }
  632.         pt->nextsym = nil;
  633.         if (lastassoc)
  634.             lastassoc->nextsym = pt;
  635.         else
  636.             assoclst = pt;
  637.         lastassoc = pt;
  638.         return;
  639. }
  640.  
  641.  
  642.     static void
  643. getchaccess(login, command)
  644.     char const *login;
  645.     enum changeaccess command;
  646. {
  647.     register struct chaccess *pt;
  648.  
  649.     *nextchaccess = pt = talloc(struct chaccess);
  650.     pt->login = login;
  651.     pt->command = command;
  652.     pt->nextchaccess = nil;
  653.     nextchaccess = &pt->nextchaccess;
  654. }
  655.  
  656.  
  657.  
  658.     static void
  659. getaccessor(opt, command)
  660.     char *opt;
  661.     enum changeaccess command;
  662. /*   Function:  get the accessor list of options -e and -a,     */
  663. /*        and store in chaccess                */
  664.  
  665.  
  666. {
  667.         register c;
  668.     register char *sp;
  669.  
  670.     sp = opt;
  671.         while( ( c = *++sp) == ' ' || c == '\n' || c == '\t' || c == ',') ;
  672.         if ( c == '\0') {
  673.         if (command == erase  &&  sp-opt == 1) {
  674.         getchaccess((char const*)nil, command);
  675.         return;
  676.         }
  677.         error("missing login name after option -a or -e");
  678.         return;
  679.         }
  680.  
  681.         while( c != '\0') {
  682.         getchaccess(sp, command);
  683.         sp = checkid(sp,',');
  684.         c = *sp;   *sp = '\0';
  685.                 while( c == ' ' || c == '\n' || c == '\t'|| c == ',')c =(*++sp);
  686.         }
  687. }
  688.  
  689.  
  690.     static void
  691. getmessage(option)
  692.     char *option;
  693. {
  694.     struct Message *pt;
  695.     struct cbuf cb;
  696.     char *m;
  697.  
  698.     if (!(m = strchr(option, ':'))) {
  699.         error("-m option lacks revision number");
  700.         return;
  701.     }
  702.     *m++ = 0;
  703.     cb = cleanlogmsg(m, strlen(m));
  704.     if (!cb.size) {
  705.         error("-m option lacks log message");
  706.         return;
  707.     }
  708.     pt = talloc(struct Message);
  709.     pt->revno = option;
  710.     pt->message = cb;
  711.     pt->nextmessage = 0;
  712.     if (lastmessage)
  713.         lastmessage->nextmessage = pt;
  714.     else
  715.         messagelst = pt;
  716.     lastmessage = pt;
  717. }
  718.  
  719.  
  720.     static void
  721. getstates(sp)
  722. char    *sp;
  723. /*   Function:  get one state attribute and the corresponding   */
  724. /*              revision and store in statelst                  */
  725.  
  726. {
  727.     char const *temp;
  728.         struct  Status  *pt;
  729.         register        c;
  730.  
  731.         while( (c=(*++sp)) ==' ' || c == '\t' || c == '\n')  ;
  732.         temp = sp;
  733.     sp = checkid(sp,':');  /* check for invalid state attribute */
  734.     c = *sp;   *sp = '\0';
  735.         while( c == ' ' || c == '\t' || c == '\n' )  c = *++sp;
  736.  
  737.         if ( c == '\0' ) {  /*  change state of def. branch or Head  */
  738.             chgheadstate = true;
  739.             headstate  = temp;
  740.             return;
  741.         }
  742.         else if ( c != ':' ) {
  743.         error("missing ':' after state in option -s");
  744.             return;
  745.         }
  746.  
  747.         while( (c = *++sp) == ' ' || c == '\t' || c == '\n')  ;
  748.     pt = talloc(struct Status);
  749.         pt->status     = temp;
  750.         pt->revno      = sp;
  751.         pt->nextstatus = nil;
  752.         if (laststate)
  753.             laststate->nextstatus = pt;
  754.         else
  755.             statelst = pt;
  756.         laststate = pt;
  757. }
  758.  
  759.  
  760.  
  761.     static void
  762. getdelrev(sp)
  763. char    *sp;
  764. /*   Function:  get revision range or branch to be deleted,     */
  765. /*              and place in delrev                             */
  766. {
  767.         int    c;
  768.         struct  delrevpair      *pt;
  769.     int separator;
  770.  
  771.     pt = &delrev;
  772.         while((c = (*++sp)) == ' ' || c == '\n' || c == '\t') ;
  773.  
  774.     /* Support old ambiguous '-' syntax; this will go away.  */
  775.     if (strchr(sp,':'))
  776.         separator = ':';
  777.     else {
  778.         if (strchr(sp,'-')  &&  VERSION(5) <= RCSversion)
  779.             warn("`-' is obsolete in `-o%s'; use `:' instead", sp);
  780.         separator = '-';
  781.     }
  782.  
  783.     if (c == separator) { /* -o:rev */
  784.             while( (c = (*++sp)) == ' ' || c == '\n' || c == '\t')  ;
  785.             pt->strt = sp;    pt->code = 1;
  786.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0') c =(*++sp);
  787.             *sp = '\0';
  788.         pt->end = nil;
  789.             return;
  790.         }
  791.         else {
  792.             pt->strt = sp;
  793.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0'
  794.            && c != separator )  c = *++sp;
  795.             *sp = '\0';
  796.             while( c == ' ' || c == '\n' || c == '\t' )  c = *++sp;
  797.             if ( c == '\0' )  {  /*   -o rev or branch   */
  798.                 pt->end = nil;   pt->code = 0;
  799.                 return;
  800.             }
  801.         if (c != separator) {
  802.         faterror("invalid range %s %s after -o", pt->strt, sp);
  803.             }
  804.             while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  805.         if (!c) {  /* -orev: */
  806.                 pt->end = nil;   pt->code = 2;
  807.                 return;
  808.             }
  809.         }
  810.     /* -orev1:rev2 */
  811.     pt->end = sp;  pt->code = 3;
  812.         while( c!= ' ' && c != '\n' && c != '\t' && c != '\0') c = *++sp;
  813.         *sp = '\0';
  814. }
  815.  
  816.  
  817.  
  818.  
  819.     static void
  820. scanlogtext(delta,edit)
  821.     struct hshentry *delta;
  822.     int edit;
  823. /* Function: Scans delta text nodes up to and including the one given
  824.  * by delta, or up to last one present, if delta==nil.
  825.  * For the one given by delta (if delta!=nil), the log message is saved into
  826.  * delta->log if delta==cuttail; the text is edited if EDIT is set, else copied.
  827.  * Assumes the initial lexeme must be read in first.
  828.  * Does not advance nexttok after it is finished, except if delta==nil.
  829.  */
  830. {
  831.     struct hshentry const *nextdelta;
  832.     struct cbuf cb;
  833.  
  834.     for (;;) {
  835.         foutptr = 0;
  836.         if (eoflex()) {
  837.                     if(delta)
  838.             faterror("can't find delta for revision %s", delta->num);
  839.             return; /* no more delta text nodes */
  840.                 }
  841.         nextlex();
  842.         if (!(nextdelta=getnum()))
  843.             faterror("delta number corrupted");
  844.         if (nextdelta->selector) {
  845.             foutptr = frewrite;
  846.             aprintf(frewrite,DELNUMFORM,nextdelta->num,Klog);
  847.                 }
  848.         getkeystring(Klog);
  849.         if (nextdelta == cuttail) {
  850.             cb = savestring(&curlogbuf);
  851.             if (!delta->log.string)
  852.                 delta->log = cleanlogmsg(curlogbuf.string, cb.size);
  853.         } else if (nextdelta->log.string && nextdelta->selector) {
  854.             foutptr = 0;
  855.             readstring();
  856.             foutptr = frewrite;
  857.             putstring(foutptr, false, nextdelta->log, true);
  858.             afputc(nextc, foutptr);
  859.                 } else {readstring();
  860.                 }
  861.                 nextlex();
  862.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  863.             ignorephrase();
  864.         getkeystring(Ktext);
  865.  
  866.         if (delta==nextdelta)
  867.             break;
  868.         readstring(); /* skip over it */
  869.  
  870.     }
  871.     /* got the one we're looking for */
  872.     if (edit)
  873.         editstring((struct hshentry *)nil);
  874.     else
  875.         enterstring();
  876. }
  877.  
  878.  
  879.  
  880.     static struct Lockrev *
  881. rmnewlocklst(which)
  882.     struct Lockrev const *which;
  883. /*   Function:  remove lock to revision which->revno from newlocklst   */
  884.  
  885. {
  886.         struct  Lockrev   * pt, *pre;
  887.  
  888.         while( newlocklst && (! strcmp(newlocklst->revno, which->revno))){
  889.         struct Lockrev *pn = newlocklst->nextrev;
  890.         tfree(newlocklst);
  891.         newlocklst = pn;
  892.         }
  893.  
  894.         pt = pre = newlocklst;
  895.         while( pt ) {
  896.             if ( ! strcmp(pt->revno, which->revno) ) {
  897.                 pre->nextrev = pt->nextrev;
  898.         tfree(pt);
  899.         pt = pre->nextrev;
  900.             }
  901.             else {
  902.                 pre = pt;
  903.                 pt = pt->nextrev;
  904.             }
  905.         }
  906.         return pre;
  907. }
  908.  
  909.  
  910.  
  911.     static void
  912. doaccess()
  913. {
  914.     register struct chaccess *ch;
  915.     register struct access **p, *t;
  916.  
  917.     for (ch = chaccess;  ch;  ch = ch->nextchaccess) {
  918.         switch (ch->command) {
  919.         case erase:
  920.             if (!ch->login)
  921.                 AccessList = nil;
  922.             else
  923.                 for (p = &AccessList;  (t = *p);  )
  924.                 if (strcmp(ch->login, t->login) == 0)
  925.                     *p = t->nextaccess;
  926.                 else
  927.                     p = &t->nextaccess;
  928.             break;
  929.         case append:
  930.             for (p = &AccessList;  ;  p = &t->nextaccess)
  931.                 if (!(t = *p)) {
  932.                     *p = t = ftalloc(struct access);
  933.                     t->login = ch->login;
  934.                     t->nextaccess = nil;
  935.                     break;
  936.                 } else if (strcmp(ch->login, t->login) == 0)
  937.                     break;
  938.             break;
  939.         }
  940.     }
  941. }
  942.  
  943.  
  944.     static int
  945. sendmail(Delta, who)
  946.     char const *Delta, *who;
  947. /*   Function:  mail to who, informing him that his lock on delta was
  948.  *   broken by caller. Ask first whether to go ahead. Return false on
  949.  *   error or if user decides not to break the lock.
  950.  */
  951. {
  952. #ifdef SENDMAIL
  953.     char const *messagefile;
  954.     int old1, old2, c;
  955.         FILE    * mailmess;
  956. #endif
  957.  
  958.  
  959.     aprintf(stderr, "Revision %s is already locked by %s.\n", Delta, who);
  960.     if (!yesorno(false, "Do you want to break the lock? [ny](n): "))
  961.         return false;
  962.  
  963.         /* go ahead with breaking  */
  964. #ifdef SENDMAIL
  965.     messagefile = maketemp(0);
  966.     if (!(mailmess = fopen(messagefile, "w"))) {
  967.         efaterror(messagefile);
  968.         }
  969.  
  970.     aprintf(mailmess, "Subject: Broken lock on %s\n\nYour lock on revision %s of file %s\nhas been broken by %s for the following reason:\n",
  971.         basename(RCSfilename), Delta, getfullRCSname(), getcaller()
  972.     );
  973.     aputs("State the reason for breaking the lock:\n(terminate with single '.' or end of file)\n>> ", stderr);
  974.  
  975.         old1 = '\n';    old2 = ' ';
  976.         for (; ;) {
  977.         c = getcstdin();
  978.         if (feof(stdin)) {
  979.         aprintf(mailmess, "%c\n", old1);
  980.                 break;
  981.             }
  982.             else if ( c == '\n' && old1 == '.' && old2 == '\n')
  983.                 break;
  984.             else {
  985.         afputc(old1, mailmess);
  986.                 old2 = old1;   old1 = c;
  987.         if (c=='\n') aputs(">> ", stderr);
  988.             }
  989.         }
  990.     Ozclose(&mailmess);
  991.  
  992.     if (run(messagefile, (char*)nil, SENDMAIL, who, (char*)nil))
  993.         warn("Mail may have failed."),
  994. #else
  995.         warn("Mail notification of broken locks is not available."),
  996. #endif
  997.         warn("Please tell `%s' why you broke the lock.", who);
  998.     return(true);
  999. }
  1000.  
  1001.  
  1002.  
  1003.     static void
  1004. breaklock(delta)
  1005.     struct hshentry const *delta;
  1006. /* function: Finds the lock held by caller on delta,
  1007.  * and removes it.
  1008.  * Sends mail if a lock different from the caller's is broken.
  1009.  * Prints an error message if there is no such lock or error.
  1010.  */
  1011. {
  1012.         register struct lock * next, * trail;
  1013.     char const *num;
  1014.         struct lock dummy;
  1015.  
  1016.     num=delta->num;
  1017.         dummy.nextlock=next=Locks;
  1018.         trail = &dummy;
  1019.         while (next!=nil) {
  1020.         if (strcmp(num, next->delta->num) == 0) {
  1021.             if (
  1022.                 strcmp(getcaller(),next->login) != 0
  1023.                 &&    !sendmail(num, next->login)
  1024.             ) {
  1025.                 error("%s still locked by %s", num, next->login);
  1026.                 return;
  1027.             }
  1028.             break; /* exact match */
  1029.                 }
  1030.                 trail=next;
  1031.                 next=next->nextlock;
  1032.         }
  1033.         if (next!=nil) {
  1034.                 /*found one */
  1035.         diagnose("%s unlocked\n",next->delta->num);
  1036.                 trail->nextlock=next->nextlock;
  1037.                 next->delta->lockedby=nil;
  1038.                 Locks=dummy.nextlock;
  1039.         } else  {
  1040.         error("no lock set on revision %s", num);
  1041.         }
  1042. }
  1043.  
  1044.  
  1045.  
  1046.     static struct hshentry *
  1047. searchcutpt(object, length, store)
  1048.     char const *object;
  1049.     unsigned length;
  1050.     struct hshentries *store;
  1051. /*   Function:  Search store and return entry with number being object. */
  1052. /*              cuttail = nil, if the entry is Head; otherwise, cuttail */
  1053. /*              is the entry point to the one with number being object  */
  1054.  
  1055. {
  1056.     cuthead = nil;
  1057.     while (compartial(store->first->num, object, length)) {
  1058.         cuthead = store->first;
  1059.         store = store->rest;
  1060.     }
  1061.     return store->first;
  1062. }
  1063.  
  1064.  
  1065.  
  1066.     static int
  1067. branchpoint(strt, tail)
  1068. struct  hshentry        *strt,  *tail;
  1069. /*   Function: check whether the deltas between strt and tail    */
  1070. /*        are locked or branch point, return 1 if any is  */
  1071. /*        locked or branch point; otherwise, return 0 and */
  1072. /*        mark deleted                    */
  1073.  
  1074. {
  1075.         struct  hshentry    *pt;
  1076.     struct lock const *lockpt;
  1077.         int     flag;
  1078.  
  1079.  
  1080.         pt = strt;
  1081.         flag = false;
  1082.         while( pt != tail) {
  1083.             if ( pt->branches ){ /*  a branch point  */
  1084.                 flag = true;
  1085.         error("can't remove branch point %s", pt->num);
  1086.             }
  1087.         lockpt = Locks;
  1088.         while(lockpt && lockpt->delta != pt)
  1089.         lockpt = lockpt->nextlock;
  1090.         if ( lockpt ) {
  1091.         flag = true;
  1092.         error("can't remove locked revision %s",pt->num);
  1093.         }
  1094.             pt = pt->next;
  1095.         }
  1096.  
  1097.         if ( ! flag ) {
  1098.             pt = strt;
  1099.             while( pt != tail ) {
  1100.         pt->selector = false;
  1101.         diagnose("deleting revision %s\n",pt->num);
  1102.                 pt = pt->next;
  1103.             }
  1104.         }
  1105.         return flag;
  1106. }
  1107.  
  1108.  
  1109.  
  1110.     static int
  1111. removerevs()
  1112. /*   Function:  get the revision range to be removed, and place the     */
  1113. /*              first revision removed in delstrt, the revision before  */
  1114. /*              delstrt in cuthead( nil, if delstrt is head), and the   */
  1115. /*              revision after the last removed revision in cuttail(nil */
  1116. /*              if the last is a leaf                                   */
  1117.  
  1118. {
  1119.     struct  hshentry *target, *target2, *temp;
  1120.     unsigned length;
  1121.     int flag;
  1122.  
  1123.         flag = false;
  1124.     if (!expandsym(delrev.strt, &numrev)) return 0;
  1125.     target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1126.         if ( ! target ) return 0;
  1127.     if (cmpnum(target->num, numrev.string)) flag = true;
  1128.     length = countnumflds(numrev.string);
  1129.  
  1130.     if (delrev.code == 0) {  /*  -o  rev    or    -o  branch   */
  1131.         if (length & 1)
  1132.         temp=searchcutpt(target->num,length+1,gendeltas);
  1133.         else if (flag) {
  1134.         error("Revision %s doesn't exist.", numrev.string);
  1135.         return 0;
  1136.         }
  1137.         else
  1138.         temp = searchcutpt(numrev.string, length, gendeltas);
  1139.         cuttail = target->next;
  1140.             if ( branchpoint(temp, cuttail) ) {
  1141.                 cuttail = nil;
  1142.                 return 0;
  1143.             }
  1144.             delstrt = temp;     /* first revision to be removed   */
  1145.             return 1;
  1146.         }
  1147.  
  1148.     if (length & 1) {   /*  invalid branch after -o   */
  1149.         error("invalid branch range %s after -o", numrev.string);
  1150.             return 0;
  1151.         }
  1152.  
  1153.     if (delrev.code == 1) {  /*  -o  -rev   */
  1154.             if ( length > 2 ) {
  1155.                 temp = searchcutpt( target->num, length-1, gendeltas);
  1156.                 cuttail = target->next;
  1157.             }
  1158.             else {
  1159.                 temp = searchcutpt(target->num, length, gendeltas);
  1160.                 cuttail = target;
  1161.                 while( cuttail && ! cmpnumfld(target->num,cuttail->num,1) )
  1162.                     cuttail = cuttail->next;
  1163.             }
  1164.             if ( branchpoint(temp, cuttail) ){
  1165.                 cuttail = nil;
  1166.                 return 0;
  1167.             }
  1168.             delstrt = temp;
  1169.             return 1;
  1170.         }
  1171.  
  1172.     if (delrev.code == 2) {   /*  -o  rev-   */
  1173.             if ( length == 2 ) {
  1174.                 temp = searchcutpt(target->num, 1,gendeltas);
  1175.                 if ( flag)
  1176.                     cuttail = target;
  1177.                 else
  1178.                     cuttail = target->next;
  1179.             }
  1180.             else  {
  1181.                 if ( flag){
  1182.                     cuthead = target;
  1183.                     if ( !(temp = target->next) ) return 0;
  1184.                 }
  1185.                 else
  1186.                     temp = searchcutpt(target->num, length, gendeltas);
  1187.         getbranchno(temp->num, &numrev);  /* get branch number */
  1188.         target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1189.             }
  1190.             if ( branchpoint( temp, cuttail ) ) {
  1191.                 cuttail = nil;
  1192.                 return 0;
  1193.             }
  1194.             delstrt = temp;
  1195.             return 1;
  1196.         }
  1197.  
  1198.         /*   -o   rev1-rev2   */
  1199.     if (!expandsym(delrev.end, &numrev)) return 0;
  1200.     if (
  1201.         length != countnumflds(numrev.string)
  1202.         ||    length>2 && compartial(numrev.string, target->num, length-1)
  1203.     ) {
  1204.         error("invalid revision range %s-%s", target->num, numrev.string);
  1205.             return 0;
  1206.         }
  1207.  
  1208.     target2 = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas);
  1209.         if ( ! target2 ) return 0;
  1210.  
  1211.         if ( length > 2) {  /* delete revisions on branches  */
  1212.             if ( cmpnum(target->num, target2->num) > 0) {
  1213.         if (cmpnum(target2->num, numrev.string))
  1214.                     flag = true;
  1215.                 else
  1216.                     flag = false;
  1217.                 temp = target;
  1218.                 target = target2;
  1219.                 target2 = temp;
  1220.             }
  1221.             if ( flag ) {
  1222.                 if ( ! cmpnum(target->num, target2->num) ) {
  1223.             error("Revisions %s-%s don't exist.", delrev.strt,delrev.end);
  1224.                     return 0;
  1225.                 }
  1226.                 cuthead = target;
  1227.                 temp = target->next;
  1228.             }
  1229.             else
  1230.                 temp = searchcutpt(target->num, length, gendeltas);
  1231.             cuttail = target2->next;
  1232.         }
  1233.         else { /*  delete revisions on trunk  */
  1234.             if ( cmpnum( target->num, target2->num) < 0 ) {
  1235.                 temp = target;
  1236.                 target = target2;
  1237.                 target2 = temp;
  1238.             }
  1239.             else
  1240.         if (cmpnum(target2->num, numrev.string))
  1241.                     flag = true;
  1242.                 else
  1243.                     flag = false;
  1244.             if ( flag ) {
  1245.                 if ( ! cmpnum(target->num, target2->num) ) {
  1246.             error("Revisions %s-%s don't exist.", delrev.strt, delrev.end);
  1247.                     return 0;
  1248.                 }
  1249.                 cuttail = target2;
  1250.             }
  1251.             else
  1252.                 cuttail = target2->next;
  1253.             temp = searchcutpt(target->num, length, gendeltas);
  1254.         }
  1255.         if ( branchpoint(temp, cuttail) )  {
  1256.             cuttail = nil;
  1257.             return 0;
  1258.         }
  1259.         delstrt = temp;
  1260.         return 1;
  1261. }
  1262.  
  1263.  
  1264.  
  1265.     static void
  1266. doassoc()
  1267. /*   Function: add or delete(if revno is nil) association    */
  1268. /*        which is stored in assoclst            */
  1269.  
  1270. {
  1271.     char const *p;
  1272.     struct Symrev const *curassoc;
  1273.     struct  assoc   * pre,  * pt;
  1274.  
  1275.         /*  add new associations   */
  1276.         curassoc = assoclst;
  1277.         while( curassoc ) {
  1278.             if ( curassoc->revno == nil ) {  /* delete symbol  */
  1279.         pre = pt = Symbols;
  1280.                 while( pt && strcmp(pt->symbol,curassoc->ssymbol) ) {
  1281.             pre = pt;
  1282.             pt = pt->nextassoc;
  1283.         }
  1284.         if ( pt )
  1285.             if ( pre == pt )
  1286.             Symbols = pt->nextassoc;
  1287.             else
  1288.             pre->nextassoc = pt->nextassoc;
  1289.         else
  1290.             warn("can't delete nonexisting symbol %s",curassoc->ssymbol);
  1291.         }
  1292.         else {
  1293.         if (curassoc->revno[0]) {
  1294.             p = 0;
  1295.             if (expandsym(curassoc->revno, &numrev))
  1296.             p = fstr_save(numrev.string);
  1297.         } else if (!(p = tiprev()))
  1298.             error("no latest revision to associate with symbol %s",
  1299.                 curassoc->ssymbol
  1300.             );
  1301.         if (p)
  1302.             VOID addsymbol(p, curassoc->ssymbol, curassoc->override);
  1303.         }
  1304.             curassoc = curassoc->nextsym;
  1305.         }
  1306.  
  1307. }
  1308.  
  1309.  
  1310.  
  1311.     static void
  1312. dolocks()
  1313. /* Function: remove lock for caller or first lock if unlockcaller is set;
  1314.  *           remove locks which are stored in rmvlocklst,
  1315.  *           add new locks which are stored in newlocklst,
  1316.  *           add lock for Dbranch or Head if lockhead is set.
  1317.  */
  1318. {
  1319.     struct Lockrev const *lockpt;
  1320.     struct hshentry *target;
  1321.  
  1322.     if (unlockcaller) { /*  find lock for caller  */
  1323.             if ( Head ) {
  1324.         if (Locks) {
  1325.             switch (findlock(true, &target)) {
  1326.               case 0:
  1327.             breaklock(Locks->delta); /* remove most recent lock */
  1328.             break;
  1329.               case 1:
  1330.             diagnose("%s unlocked\n",target->num);
  1331.             break;
  1332.             }
  1333.         } else {
  1334.             warn("No locks are set.");
  1335.         }
  1336.             } else {
  1337.         warn("can't unlock an empty tree");
  1338.             }
  1339.         }
  1340.  
  1341.         /*  remove locks which are stored in rmvlocklst   */
  1342.         lockpt = rmvlocklst;
  1343.         while( lockpt ) {
  1344.         if (expandsym(lockpt->revno, &numrev)) {
  1345.         target = genrevs(numrev.string, (char *)nil, (char *)nil, (char *)nil, &gendeltas);
  1346.                 if ( target )
  1347.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1348.             error("can't unlock nonexisting revision %s",lockpt->revno);
  1349.                    else
  1350.             breaklock(target);
  1351.                         /* breaklock does its own diagnose */
  1352.             }
  1353.             lockpt = lockpt->nextrev;
  1354.         }
  1355.  
  1356.         /*  add new locks which stored in newlocklst  */
  1357.         lockpt = newlocklst;
  1358.         while( lockpt ) {
  1359.         setlock(lockpt->revno);
  1360.             lockpt = lockpt->nextrev;
  1361.         }
  1362.  
  1363.     if (lockhead) {  /*  lock default branch or head  */
  1364.             if (Dbranch) {
  1365.         setlock(Dbranch);
  1366.         } else if (Head) {
  1367.         if (0 <= addlock(Head))
  1368.             diagnose("%s locked\n",Head->num);
  1369.             } else {
  1370.         warn("can't lock an empty tree");
  1371.             }
  1372.         }
  1373.  
  1374. }
  1375.  
  1376.  
  1377.  
  1378.     static void
  1379. setlock(rev)
  1380.     char const *rev;
  1381. /* Function: Given a revision or branch number, finds the corresponding
  1382.  * delta and locks it for caller.
  1383.  */
  1384. {
  1385.         struct  hshentry *target;
  1386.  
  1387.     if (expandsym(rev, &numrev)) {
  1388.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1389.                  (char*)nil, &gendeltas);
  1390.             if ( target )
  1391.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1392.             error("can't lock nonexisting revision %s", numrev.string);
  1393.                else
  1394.             if (0 <= addlock(target))
  1395.             diagnose("%s locked\n", target->num);
  1396.         }
  1397. }
  1398.  
  1399.  
  1400.     static void
  1401. domessages()
  1402. {
  1403.     struct hshentry *target;
  1404.     struct Message *p;
  1405.  
  1406.     for (p = messagelst;  p;  p = p->nextmessage)
  1407.         if (
  1408.         expandsym(p->revno, &numrev)  &&
  1409.         (target = genrevs(
  1410.             numrev.string, (char*)0, (char*)0, (char*)0, &gendeltas
  1411.         ))
  1412.         )
  1413.         target->log = p->message;
  1414. }
  1415.  
  1416.  
  1417.     static void
  1418. rcs_setstate(rev,status)
  1419.     char const *rev, *status;
  1420. /* Function: Given a revision or branch number, finds the corresponding delta
  1421.  * and sets its state to status.
  1422.  */
  1423. {
  1424.         struct  hshentry *target;
  1425.  
  1426.     if (expandsym(rev, &numrev)) {
  1427.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1428.                  (char*)nil, &gendeltas);
  1429.             if ( target )
  1430.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1431.             error("can't set state of nonexisting revision %s to %s",
  1432.               numrev.string, status);
  1433.                else
  1434.                     target->state = status;
  1435.         }
  1436. }
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.     static int
  1443. buildeltatext(deltas)
  1444.     struct hshentries const *deltas;
  1445. /*   Function:  put the delta text on frewrite and make necessary   */
  1446. /*              change to delta text                                */
  1447. {
  1448.     register FILE *fcut;    /* temporary file to rebuild delta tree */
  1449.     char const *cutfilename, *diffilename;
  1450.  
  1451.     cutfilename = nil;
  1452.     cuttail->selector = false;
  1453.     scanlogtext(deltas->first, false);
  1454.         if ( cuthead )  {
  1455.         cutfilename = maketemp(3);
  1456.         if (!(fcut = fopen(cutfilename, FOPEN_W_WORK))) {
  1457.         efaterror(cutfilename);
  1458.             }
  1459.  
  1460.         while (deltas->first != cuthead) {
  1461.         deltas = deltas->rest;
  1462.         scanlogtext(deltas->first, true);
  1463.             }
  1464.  
  1465.         snapshotedit(fcut);
  1466.         Ofclose(fcut);
  1467.         }
  1468.  
  1469.     while (deltas->first != cuttail)
  1470.         scanlogtext((deltas = deltas->rest)->first, true);
  1471.     finishedit((struct hshentry *)nil, (FILE*)0, true);
  1472.     Ozclose(&fcopy);
  1473.  
  1474.         if ( cuthead ) {
  1475.         diffilename = maketemp(0);
  1476.         switch (run((char*)nil,diffilename,
  1477.             DIFF DIFF_FLAGS, cutfilename, resultfile, (char*)nil
  1478.         )) {
  1479.         case DIFF_FAILURE: case DIFF_SUCCESS: break;
  1480.         default: faterror ("diff failed");
  1481.         }
  1482.         return putdtext(cuttail->num,cuttail->log,diffilename,frewrite,true);
  1483.     } else
  1484.         return putdtext(cuttail->num,cuttail->log,resultfile,frewrite,false);
  1485. }
  1486.  
  1487.  
  1488.  
  1489.     static void
  1490. buildtree()
  1491. /*   Function:  actually removes revisions whose selector field  */
  1492. /*        is false, and rebuilds the linkage of deltas.     */
  1493. /*              asks for reconfirmation if deleting last revision*/
  1494. {
  1495.     struct    hshentry   * Delta;
  1496.         struct  branchhead      *pt, *pre;
  1497.  
  1498.         if ( cuthead )
  1499.            if ( cuthead->next == delstrt )
  1500.                 cuthead->next = cuttail;
  1501.            else {
  1502.                 pre = pt = cuthead->branches;
  1503.                 while( pt && pt->hsh != delstrt )  {
  1504.                     pre = pt;
  1505.                     pt = pt->nextbranch;
  1506.                 }
  1507.                 if ( cuttail )
  1508.                     pt->hsh = cuttail;
  1509.                 else if ( pt == pre )
  1510.                     cuthead->branches = pt->nextbranch;
  1511.                 else
  1512.                     pre->nextbranch = pt->nextbranch;
  1513.             }
  1514.     else {
  1515.             if ( cuttail == nil && !quietflag) {
  1516.         if (!yesorno(false, "Do you really want to delete all revisions? [ny](n): ")) {
  1517.             error("No revision deleted");
  1518.             Delta = delstrt;
  1519.             while( Delta) {
  1520.             Delta->selector = true;
  1521.             Delta = Delta->next;
  1522.             }
  1523.             return;
  1524.         }
  1525.         }
  1526.             Head = cuttail;
  1527.     }
  1528.         return;
  1529. }
  1530.  
  1531. #if lint
  1532. /* This lets us lint everything all at once. */
  1533.  
  1534. char const cmdid[] = "";
  1535.  
  1536. #define go(p,e) {int p P((int,char**)); void e P((void)); if(*argv)return p(argc,argv);if(*argv[1])e();}
  1537.  
  1538.     int
  1539. main(argc, argv)
  1540.     int argc;
  1541.     char **argv;
  1542. {
  1543.     go(ciId,    ciExit);
  1544.     go(coId,    coExit);
  1545.     go(identId,    identExit);
  1546.     go(mergeId,    mergeExit);
  1547.     go(rcsId,    exiterr);
  1548.     go(rcscleanId,    rcscleanExit);
  1549.     go(rcsdiffId,    rdiffExit);
  1550.     go(rcsmergeId,    rmergeExit);
  1551.     go(rlogId,    rlogExit);
  1552.     return 0;
  1553. }
  1554. #endif
  1555.